﻿using System.Web.Mvc;
using Microsoft.Practices.Unity;
using WebShop.DAL;
using WebShop.DAL.Interfaces;

namespace WebShop.Infrastructure
{
    public class HandleErrorsAttribute : HandleErrorAttribute
    {
        ILogRepository m_LogRepository;
        ILogRepository LogRepository
        {
            get
            {
                if (m_LogRepository == null)
                {
                    m_LogRepository = DependencyResolver.Current.GetService<ILogRepository>();
                }
                return m_LogRepository;
            }
        }

        public override void OnException(ExceptionContext filterContext)
        {
            base.OnException(filterContext);

            var exception = filterContext.Exception;
            var httpContext = filterContext.HttpContext;

            LogRepository.Add(exception, httpContext.User.Identity.Name);
        }
    }
}
